package de.syfy.project.engine.hud.interfaces;

import de.syfy.project.engine.Texture;
import de.syfy.project.engine.TextureLoader;
import de.syfy.project.engine.hud.EventListener;
import de.syfy.project.engine.interfaces.Game;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.lwjgl.input.Mouse;

/**
 *
 * @author Timo
 * 
 * Ausführlichere Info folgt
 */
public abstract class UI_Item implements Comparable {

    private static List<UI_Item> items;
    private static Game game;

    public UI_Item(Game g) {
        if (items == null || items.isEmpty()) {
            game = g;
            items = Collections.synchronizedList(new ArrayList<UI_Item>());
            items.add(this);
            new Thread() {

                @Override
                public void run() {
                    update();
                }
            }.start();
        } else {
            items.add(this);
        }
    }

    private synchronized void update() {
        while ((items != null || !items.isEmpty()) && game.isGameRunning()) {
            if (Mouse.isCreated() && Mouse.isInsideWindow()) {
                float x = Mouse.getX();
                float y = Mouse.getY();
                for(UI_Item item: items){
                    item.itemHover(x, y);
                }
                while (Mouse.next()) {
                    for (UI_Item item : items) {
                        //Linksklick mit der Maus
                        if (Mouse.getEventButtonState() && Mouse.getEventButton() == 0) {
                            item.itemClick(x, y);
                        }
                    }
                }
            }
            try {
                Thread.sleep(32);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    /*
     * Die Grafik für das Objekt
     */
    public abstract void setTexture(Texture texture);
    
    public abstract void setTextures(Texture standard, Texture pressed, Texture highlight);
    
    public abstract void setStandardTextures(Texture texture);

    public abstract void setStandardTextures(TextureLoader loader, String path, int x, int y, int width, int height);
    
    public abstract void setPressedTextures(TextureLoader loader, String path, int x, int y, int width, int height);
    
    public abstract void setPressedTextures(Texture texture);
    
    public abstract void setHighlightTextures(TextureLoader loader, String path, int x, int y, int width, int height);
    
    public abstract void setHighlightTextures(Texture texture);
    
    public abstract void setTexture(TextureLoader loader, String path);

    public abstract Texture getTexture();

    public abstract void setSize(float width, float height);

    public abstract void setWidth(float width);

    public abstract void setHeight(float height);

    public abstract float getWidth();

    public abstract float getHeight();

    /*
     * Position ist relativ zu der Position des Containers.
     */
    public abstract void setPosition(Point2D.Float p);

    public abstract Point2D.Float getPosition();

    /*
     * Setzt die Priorität dieses UI-Items. Wird später genutzt um 
     * die Reihenfolge des Renderns herrauszufinden.
     */
    public abstract void setPriority(int priority);

    public abstract int getPriority();

    /*
     * Wird aufgerufen, wenn das Objekt geklickt wird.
     */
    public abstract void itemClick(float x, float y);

    /*
     * Wird aufgerufen, wenn der Mauszeiger über das Item kommt
     */
    public abstract void itemHover(float x, float y);

    public abstract void setParent(UI_Container parent);

    public abstract UI_Container getParent();

    public abstract void drawItem();

    public abstract void addEventListener(EventListener listener);
}
